/**
 * Created by zheng on 2018/7/2.
 */
var deleteindex = null
//当前保单所分配的状态ID
var state = 1

$(function () {
    //var binbin = 0;
    $('#dg').datagrid({
        url: '/insurancebill/queryOdd?state=' + state,
        fit: true,
        fitColumns: true,
        //分页条
        pagination: true,
        // fit:true,
        //工具栏
        toolbar: "#tb",
        //单选
        singleSelect: true,
        columns: [[
            {field: 'x', checkbox: 'true'},
            {field: 'oddNumber', title: '当前状态', width: 100},
            {field: 'vdate', title: '保险单号', width: 100},
            {field: 'vdate', title: '联系方式', width: 100},
            {field: 'vdate', title: '报案时间', width: 100},
            {field: 'vdate', title: '事故责任', width: 100},
            {field: 'vdate', title: '验证员工', width: 100},
            {field: 'vdate', title: '备注', width: 100},
            {field: 'limitYear', title: '保险单号', width: 100}

        ]],
        border:false,
        cls:"table table-very table-basic"
    })


    //准备输入的对话弹框
    $('#dd').dialog({
        title: 'My Dialog',
        width: 700,
        height: 700,
        closed: true,
        // cache: false,
        modal: true,
        buttons: "#bb"
    })
    //订单详细
    $('#billDetail').dialog({
        title: 'My Dialog',
        width: 700,
        height: 700,
        closed: true,
        modal: true
    })
    //<!-- 准备投保人详情-->
    $('#applicantItem').dialog({
        title: 'My Dialog',
        width: 400,
        height: 500,
        closed: true,
        // cache: false,
        modal: true
    })

    //将工具栏所有的函数集合在一个对象中
    var cmdObj;
    cmdObj = {
        //刷新emp页面
        reload: function () {
            $('#dg').datagrid("load")
        },
        //改变emp状态
        changState: function () {
            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $.messager.confirm('确认','您确认想要提交数据吗？',function(r){
                if (r){
                    $.get("/insurancebill/changState", {id: row.id, state: 3}, function (data) {
                        if (data.succeed) {
                            $.messager.alert("温馨提示", "提审成功");
                            $('#dg').datagrid("reload")
                        } else {
                            $.messager.alert("温馨提示", "提审失败");
                        }
                    })
                }
            });
        },

        queryOddItem: function () {

            var row = $('#dg').datagrid("getSelected");
            console.log(row)
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $('#billDetailForm').form("clear");
            $('#billDetail').dialog("open")
            $('#billDetail').dialog("setTitle", "查看保单信息")
            console.log("row");
            console.log(row);
            //清除以前的数据

            //总价显示
            var totalPrice = row.totalPrice != null ? row.totalPrice : 0
            $("#showtotalAmount_r").text(totalPrice);
            //保险年限
            console.log("保险年限");
            console.log(row.limitYear);
            //binbin = row.limitYear;
            $("#showlimitYear_input_r").textbox('setValue', row.limitYear);


            //回显
            if (row.car) {
                $("#carId").val(row.car.id);
            }
            $('#billDetailForm').form('load', row.car);
            //单号处理回显
            $("#billId").val(row.id);

            //产品机构的的回显
            $.get("/product/queryProductListByBillId?billId=" + row.id,
                function (data) {
                    console.log("data");
                    console.log(data);
                    if (data.length>0) {
                        $("#billDetail_datagrid").datagrid('loadData', data);
                        if (data[0] != null) {
                            $("#agency_r").combobox('setValue', data[0].orgName);

                        }
                        console.log(data[1]);
                        console.log(data[0].orgName);
                        console.log(data[0].organizationId);
                    }
                })
            //暂存表中的dategrid
            $('#billDetail_datagrid').datagrid({
                fitColumns: true,
                height: 200,
                //分页条
                pagination: true,
                // fit:true,
                columns: [[
                    {field: 'id', title: '编号', hidden: true},
                    {field: 'sn', title: '保险编号', width: 100},
                    {field: 'name', title: '保险产品', width: 100},
                    {field: 'annualfee', title: '保险年费', width: 100}
                ]]
            })

        },
        queryApplicantItem: function () {

            var row = $('#dg').datagrid("getSelected");
            if (row == null) {
                $.messager.alert("温馨提示", "请选择一行");
                return;
            }
            $('#applicantItemForm').form("clear");
            $('#applicantItem').dialog("open")
            $('#applicantItem').dialog("setTitle", "查看投保单明细")
            //处理部门回显
            /*    if (row.dept) {
             row["dept.id"] = row.dept.id;

             }*/

            //客户数据字典编辑回显

            $.get("/client/get?id=" + row.applicant.id,
                function (data) {
                    if (data.clientSource != null) {
                        $("#clientSource").combobox("setValue", data.clientSource.name);

                    }
                    if (data.salaryLevel != null) {
                        $("#salary").combobox("setValue", data.salaryLevel.name);

                    }
                    if (data.job != null) {
                        $("#job").combobox("setValue", data.job.name);

                    }
                    //$("#clientSource").combobox("setValue", "kkkkk");

                    console.log("字典");
                    console.log(data);
                    console.log(data.clientSource);
                    console.log(data.salaryLevel);
                    console.log(data.job);
                });
            //字典end

            //回显
            $('#applicantItemForm').form('load', row.applicant);
            console.log(row);

        },
        //高级查询
        query: function () {
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").textbox("getValue")
            var endDate = $("#endDate").textbox("getValue");
            /*          var state = Java.type("cn.wolfcode.crm.util.CRMConstant.Insurancebill.STATE_WAIT_ADUIT");
             console.log("state");
             console.log(state);*/
            $('#dg').datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate,
                state: state
            });
        },
        //关闭按钮
        cancel: function () {
            $('#dd').dialog("close")
        }


    };

    //调用函数集合对象
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })


    //算钱的
    $("#showlimitYear_input").textbox('textbox').bind('change', function () {
        var totalAmount = 0;
        $('#showproductInfo_datagrid').datagrid('selectAll');
        var rows = $("#showproductInfo_datagrid").datagrid('getSelections');
        console.log(rows);
        if (rows) {
            for (var i = 0; i < rows.length; i++) {
                totalAmount = rows[i].annualfee * $("#showlimitYear_input").val() + totalAmount;
            }
        }
        $("#showtotalAmount").text(totalAmount);
        console.log(totalAmount);
    });


})







